package com.emmmya.ocs.config;

import lombok.Data;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * 获取当前项目的上下文环境对象
 */
@Configuration
@Component
@Data
public class WebServerConfig {
    WebServerApplicationContext appContext = null ;

    @EventListener(WebServerInitializedEvent.class)
    public void onWebServerReady(WebServerInitializedEvent event) {
        //System.out.println("1.当前WebServer实现类为：" + event.getWebServer().getClass().getName());
        WebServerApplicationContext applicationContext = event.getApplicationContext();
        if(applicationContext!= null ){
            this.appContext = applicationContext;
        }
        /**
         * 获取端口号、获取
         */
        int port = applicationContext.getWebServer().getPort();
        String name = applicationContext.getWebServer().getClass().getName();
        //System.out.println(port+":"+name);
    }
}